<?php
class ActiveDataProvider extends CActiveDataProvider {

    /**
     * SQL debug
     */
    public function debugSql($showParams = true) {

        if (!Yii::app()->params['debugMode'])
            return;

        $get = Yii::app()->request->getParam('debug');
        if (empty($get) || !in_array($get, array('sql')))
            return;

        // sort
        $this->criteria->order = implode(', ', array_filter(array(
            $this->criteria->order,
            $this->sort->getOrderBy(),
        )));

        // pagination
        $this->criteria->limit = $this->pagination->getLimit();
        $this->criteria->offset = $this->pagination->getOffset();

        // params		
        array_walk($this->criteria->params, function(&$value) {
                    $value = is_numeric($value) ? $value : "'" . $value . "'";
                });

        $sqlStatement = $this->model->getCommandBuilder()->createFindCommand($this->model->getTableSchema(), $this->criteria)->text;
        if ($showParams) {
            echo str_replace(array_keys($this->criteria->params), array_values($this->criteria->params), $sqlStatement);
        } else {
            echo $sqlStatement;
        }//end if
    }//end function

    public function eachRecord($callable, $params = array()) {
        $this->pagination->pageSize = 10;
        for ($page = 0; $page < ceil($this->totalItemCount / $this->pagination->pageSize); $page++) {
            $this->pagination->currentPage = $page;
            foreach ($this->getData(true) as $i => $data) {
                $temp = $params;
                array_unshift($temp, $data);
                call_user_func_array($callable, $temp);
            }//end foreach
        }//end for
    }//end function
}//end class